# -*- coding: utf-8 -*-

import os, sys, time, datetime, re


#teamhelper = {1:'Arizona',2:'Atlanta',3:'Baltimore',4:'Boston',5:'Chi Cubs',6:'Chi White Sox',7:'Cincinnati',8:'Cleveland',9:'Colorado',10:'Detroit',11:'Florida',12:'Houston',13:'Kansas City',14:'LA Angels',15:'LA Dodgers',16:'Milwaukee',17:'Minnesota',18:'NY Mets',19:'NY Yankees',20:'Oakland',21:'Philadelphia',22:'Pittsburgh',23:'San Diego',24:'San Francisco',25:'Seattle',26:'St. Louis',27:'Tampa Bay',28:'Texas',29:'Toronto',30:'Washington'}
#awayteam = teamhelper[awayteamid]
#hometeam = teamhelper[hometeamid]

workingdir = u'C:\\users\\blaicke\\documents\\bgbm\\boxscores_test\\'

class MLBImportBoxscore0509():
    #gets data from mlb.com boxscore files 05-09 and imports it into the database
    def __init__(self,workingdir):
        print u'init'
        filelist = self.GetFileList(workingdir)
        filetextlist = self.GetFileText(filelist)
        x = 1
        for i in filetextlist:
            #print i
            date,awayteamid,hometeamid,awayteam,hometeam = self.GetTeamsAndDate(i)
            print x,date,awayteam,hometeam
            gametype,result,awayscoreboard,homescoreboard,awayinningscore,homeinningscore = self.GetScore(i,awayteam,hometeam)
            self.GetPitchingStats(i,awayteam,hometeam)
            x+=1
            
        
    def GetFileList(self,workingdir):
        #get a list of the boxscore files
        dirlist = os.listdir(workingdir)
        filelist = []
        for i in dirlist:
            flist = os.listdir(workingdir+i)
            for ii in flist:
                filelist.append(workingdir+i+os.sep+ii)
        return filelist
    
    def GetFileText(self,filelist):
        print 'opening files'
        numberlist = []
        x = 1
        while x < 100:
            numberlist.append(x)
            x+=1
        x = float(len(filelist))
        y = 1
        filetext = []
        for i in filelist:
            for ii in numberlist:
                if float(y) == ii*round(x/100,0):
                    numberlist.remove(ii)
                    print str(ii)+'% done'
            file = open(i,'r')
            filetext.append(file.read())
            file.close()
            y+=1
        return filetext
    
    def GetTeamsAndDate(self,filetext):
        #works with boxscore 2010
        titlere = re.compile(r'<title>.*?</title>',re.DOTALL)
        titlesearch = titlere.search(filetext)
        titlestring = titlesearch.group(0)
        titlestring = titlestring.split(r' |')[0]
        titlestring = titlestring.split(r': ')[1]
        teams = titlestring.split(r' - ')[0]
        date = titlestring.split(r' - ')[1]
        year = int(date.split(r', ')[1])
        month = date.split(r' ')[0]
        day = int(date.split(r', ')[0].split(r' ')[1])
        monthhelper = {'February':2,'March':3,'April':4,'May':5,'June':6,'July':7,'August':8,'September':9,'October':10,'November':11}
        month = monthhelper[month]
        date = datetime.date(year,month,day)
        awayteam = teams.split(' vs. ')[0]
        hometeam = teams.split(' vs. ')[1]
        teamhelper = {'Arizona':1,'Atlanta':2,'Baltimore':3,'Boston':4,'Chi Cubs':5,'Chi White Sox':6,'Cincinnati':7,'Cleveland':8,'Colorado':9,'Detroit':10,'Florida':11,'Houston':12,'Kansas City':13,'LA Angels':14,'LA Dodgers':15,'Milwaukee':16,'Minnesota':17,'NY Mets':18,'NY Yankees':19,'Oakland':20,'Philadelphia':21,'Pittsburgh':22,'San Diego':23,'San Francisco':24,'Seattle':25,'St. Louis':26,'Tampa Bay':27,'Texas':28,'Toronto':29,'Washington':30}
        awayteamid = teamhelper[awayteam]
        hometeamid = teamhelper[hometeam]
        return date,awayteamid,hometeamid,awayteam,hometeam
    
    def GetScore(self,filetext,awayteam,hometeam):
        #doesn't work with boxscore 2010
        #gametype: 0 spring training, 1 regular season, 2 postseason
        #finding gametype
        if re.search(r'mlb/standings/exhibition.jsp">Standings</a>',filetext,re.DOTALL) != None:
            #spring training
            gametype = 0
            awayrecord,awaystreak = None,None
            homerecord,homestreak = None,None
        elif re.search(r'lead\s*?series,\s*?\d-\d',filetext,re.DOTALL) != None or re.search(r'series\s*?tied,\s*?\d-\d',filetext,re.DOTALL) != None or re.search(r'win\s*?series,\s*?\d-\d',filetext,re.DOTALL) != None:
            #postseason
            gametype = 2
            awayrecord,awaystreak = None,None
            homerecord,homestreak = None,None
        else:
            #regular season
            gametype = 1
            #getting the record and streak for both teams after the game
            #away
            reawayrs = re.compile(awayteam+r'\s*?\(\d.*?</td>',re.DOTALL)
            try:
                awayrs = reawayrs.search(filetext).group(0)
                #awayrecord
                awayr = awayrs.split('(')[1]
                awayr = awayr.split(')')[0]
                #awaystreak
                aways = awayrs.split('>')[1]
                aways = aways.split('<')[0]
                aways = re.search(r'[Ww]on\s*?\d+|[Ll]ost\s*?\d+',aways,re.DOTALL).group(0)
                aways = aways.split(' ')
                #converting streak to int, negative for losing streak, positive for winning
                if aways[0] == 'Won' or aways[0] == 'won':
                    aways = int(aways[1])
                elif aways[0] == 'Lost' or aways[0] == 'lost':
                    aways = -int(aways[1])
            except AttributeError:
                awayr = '0-0'
                aways = 0
            #home
            rehomers = re.compile(hometeam+r'\s*?\(\d.*?</td>',re.DOTALL)
            try:
                homers = rehomers.search(filetext).group(0)
                #homerecord
                homer = homers.split('(')[1]
                homer = homer.split(')')[0]
                #homestreak
                homes = homers.split('>')[1]
                homes = homes.split('<')[0]
                homes = re.search(r'[Ww]on\s*?\d+|[Ll]ost\s*?\d+',homes,re.DOTALL).group(0)
                homes = homes.split(' ')
                #converting streak to int, negative for losing streak, positive for winning
                if homes[0] == 'Won' or homes[0] == 'won':
                    homes = int(homes[1])
                elif homes[0] == 'Lost' or homes[0] == 'lost':
                    homes = -int(homes[1])
            except AttributeError:
                homer = '0-0'
                homes = 0
            awayrecord,awaystreak = awayr,aways
            homerecord,homestreak = homer,homes
        #getting the score for both teams
        reawayteam = re.compile(r'<b>'+awayteam+r'</b></div></td>.*?</tr>',re.DOTALL)
        rehometeam = re.compile(r'<b>'+hometeam+r'</b></div></td>.*?</tr>',re.DOTALL)
        awayscore = reawayteam.search(filetext,re.DOTALL).group(0)
        homescore = rehometeam.search(filetext,re.DOTALL).group(0)
        #getting runs
        awayR = re.search(r'<td bgcolor.*?><b>\d+?</b></td>',awayscore,re.DOTALL).group(0)
        awayR = awayR.split('>')[2]
        awayR = int(awayR.split('<')[0])
        homeR = re.search(r'<td bgcolor.*?><b>\d+?</b></td>',homescore,re.DOTALL).group(0)
        homeR = homeR.split('>')[2]
        homeR = int(homeR.split('<')[0])
        #getting which team won, 1 away, 2 home, 0 tie
        if awayR > homeR:
            result = 1
        elif awayR < homeR:
            result = 2
        elif awayR == homeR:
            result = 0
        #getting hits
        awayH = re.search(r'<td>\d*?</td>\s*?<td',awayscore,re.DOTALL).group(0)
        awayH = awayH.split('>')[1]
        awayH = int(awayH.split('<')[0])
        homeH = re.search(r'<td>\d*?</td>\s*?<td',homescore,re.DOTALL).group(0)
        homeH = homeH.split('>')[1]
        homeH = int(homeH.split('<')[0])
        #getting errors
        awayE = re.search(r'<td>\d*?</td>\s*?</tr',awayscore,re.DOTALL).group(0)
        awayE = awayE.split('>')[1]
        awayE = int(awayE.split('<')[0])
        homeE = re.search(r'<td>\d*?</td>\s*?</tr',homescore,re.DOTALL).group(0)
        homeE = homeE.split('>')[1]
        homeE = int(homeE.split('<')[0])
        #getting inningscores
        awayI = re.findall(r'<td>\s+?.*?\d+?.*?\s+?</td>|<td>\s+?.*?X.*?\s+?</td>',awayscore,re.DOTALL)
        homeI = re.findall(r'<td>\s+?.*?\d+?.*?\s+?</td>|<td>\s+?.*?X.*?\s+?</td>',homescore,re.DOTALL)
        awaystats = (awayR,awayH,awayE,awayrecord,awaystreak)
        homestats = (homeR,homeH,homeE,homerecord,homestreak)
        return gametype,result,awaystats,homestats,awayI,homeI
    
    def GetPitchingStats(self,filetext,awayteam,hometeam):
        reps = re.compile(r'<b>'+awayteam+r'</b></td>\s*?<td width="7%"><b>IP</b>.*?</table>|<b>'+hometeam+r'</b></td>\s*?<td width="7%"><b>IP</b>.*?</table>',re.DOTALL)
        ps = reps.findall(filetext)
        #print len(ps)
        awayps = ps[0]
        homeps = ps[1]
        #print awayps#,homeps
        repitcher = re.compile(r'<a target="player".*?</tr>',re.DOTALL)
        awaypitcher = repitcher.findall(awayps)
        homepitcher = repitcher.findall(homeps)
        #print len(awaypitcher),len(homepitcher)
        awaypitcherstats,homepitcherstats = [],[]
        remlbplayerid = re.compile(r'playerID=\d+?">',re.DOTALL)
        replayername = re.compile(r'playerID=\d+?">.+?</td>',re.DOTALL)
        restats = re.compile(r'playerID=\d+?">.+?<td>\d+?\.\d</td>\s*?<td>\d+?</td>\s*?<td>\d+?</td>\s*?<td>\d+?</td>\s*?<td>\d+?</td>\s*?<td>\d+?</td>\s*?<td>\d+?</td>\s*?<td align="right">.+?</td>',re.DOTALL)
        #awaypitcher playerid,playername,w/l/h/s/bs, record and number of those
        for i in awaypitcher:
            #get playerid
            mlbplayerid = remlbplayerid.search(i).group(0)
            mlbplayerid = mlbplayerid.split('=')[1]
            mlbplayerid = mlbplayerid.split('"')[0]
            #get playername
            playername = replayername.search(i).group(0)
            #get w/l/s/h first before taking that out of the playername
            #get win, loss, save, hold
            wlsh = playername.split('>')[2]
            if len(wlsh.split('(')) == 1:
                #no w/l/s/h/bs
                #first digit: 1 win,2 loss,3 hold,4 save,5 blown save,6 blown save+win,7 blown save+loss
                wlsh = (0,0,0)
            elif len(wlsh.split('(')) == 2:
                wlsh = wlsh.split('(')[1]
                recordornumber = wlsh.split(' ')[1]
                recordnumber = recordornumber.split(')')[0]
                wlsh = wlsh.split(',')[0]
                if wlsh == 'W':
                    wlsh = (1,recordnumber,0)
                elif wlsh == 'L':
                    wlsh = (2,recordnumber,0)
                elif wlsh == 'H':
                    wlsh = (3,recordnumber,0)
                elif wlsh == 'S':
                    wlsh = (4,recordnumber,0)
                elif wlsh == 'BS':
                    wlsh = (5,recordnumber,0)
            elif len(wlsh.split('(')) == 3:
                wlsh1 = wlsh.split('(')[1]
                recordornumber1 = wlsh1.split(' ')[1]
                recordornumber1 = recordornumber1.split(')')[0]
                wlsh1 = wlsh1.split(',')[0]
                wlsh2 = wlsh.split('(')[2]
                recordornumber2 = wlsh2.split(' ')[1]
                recordornumber2 = recordornumber2.split(')')[0]
                wlsh2 = wlsh2.split(',')[0]
                if wlsh2 == 'W':
                    wlsh = (6,recordornumber1,recordornumber2)
                elif wlsh2 == 'L':
                    wlsh = (7,recordornumber1,recordornumber2)
            #now further work with playername to get it cleaned up
            playername = playername.split('>')[1]
            playername = playername.split('<')[0]
            if len(playername.split(',')) > 1:
                playersurname = playername.split(',')[1].split(' ')[1]
                playername = playername.split(',')[0]
            elif len(playername.split(',')) == 1:
                playersurname = ''
            #get stats
            stats = restats.search(i).group(0)
            stats = stats.split('>')
            IP = stats[4].split('<')[0]
            H = stats[6].split('<')[0]
            R = stats[8].split('<')[0]
            ER = stats[10].split('<')[0]
            BB = stats[12].split('<')[0]
            SO = stats[14].split('<')[0]
            HR = stats[16].split('<')[0]
            ERA = stats[18].split('<')[0]
            if ERA == u'--':
                ERA = u'9999.99'
            IHelper = float(IP.split('.')[0])
            IHelper += float(IP.split('.')[1])/3
            if IHelper == 0.0:
                GameERA = u'9999.99'
            else:
                GameERA = (int(ER)*9)/IHelper
                GameERA = unicode(round(GameERA,2))
            pitcherstats = (mlbplayerid,playername,playersurname,wlsh,IP,H,R,ER,BB,SO,HR,ERA,GameERA)
            awaypitcherstats.append(pitcherstats)
        for i in homepitcher:
            #get playerid
            mlbplayerid = remlbplayerid.search(i).group(0)
            mlbplayerid = mlbplayerid.split('=')[1]
            mlbplayerid = mlbplayerid.split('"')[0]
            #get playername
            playername = replayername.search(i).group(0)
            #get w/l/s/h first before taking that out of the playername
            #get win, loss, save, hold
            wlsh = playername.split('>')[2]
            if len(wlsh.split('(')) == 1:
                #no w/l/s/h/bs
                #first digit: 1 win,2 loss,3 hold,4 save,5 blown save,6 blown save+win,7 blown save+loss
                wlsh = (0,0,0)
            elif len(wlsh.split('(')) == 2:
                wlsh = wlsh.split('(')[1]
                recordornumber = wlsh.split(' ')[1]
                recordnumber = recordornumber.split(')')[0]
                wlsh = wlsh.split(',')[0]
                if wlsh == 'W':
                    wlsh = (1,recordnumber,0)
                elif wlsh == 'L':
                    wlsh = (2,recordnumber,0)
                elif wlsh == 'H':
                    wlsh = (3,recordnumber,0)
                elif wlsh == 'S':
                    wlsh = (4,recordnumber,0)
                elif wlsh == 'BS':
                    wlsh = (5,recordnumber,0)
            elif len(wlsh.split('(')) == 3:
                wlsh1 = wlsh.split('(')[1]
                recordornumber1 = wlsh1.split(' ')[1]
                recordornumber1 = recordornumber1.split(')')[0]
                wlsh1 = wlsh1.split(',')[0]
                wlsh2 = wlsh.split('(')[2]
                recordornumber2 = wlsh2.split(' ')[1]
                recordornumber2 = recordornumber2.split(')')[0]
                wlsh2 = wlsh2.split(',')[0]
                if wlsh2 == 'W':
                    wlsh = (6,recordornumber1,recordornumber2)
                elif wlsh2 == 'L':
                    wlsh = (7,recordornumber1,recordornumber2)
            #now further work with playername to get it cleaned up
            playername = playername.split('>')[1]
            playername = playername.split('<')[0]
            if len(playername.split(',')) > 1:
                playersurname = playername.split(',')[1].split(' ')[1]
                playername = playername.split(',')[0]
            elif len(playername.split(',')) == 1:
                playersurname = ''
            #get stats
            stats = restats.search(i).group(0)
            stats = stats.split('>')
            IP = stats[4].split('<')[0]
            H = stats[6].split('<')[0]
            R = stats[8].split('<')[0]
            ER = stats[10].split('<')[0]
            BB = stats[12].split('<')[0]
            SO = stats[14].split('<')[0]
            HR = stats[16].split('<')[0]
            ERA = stats[18].split('<')[0]
            if ERA == u'--':
                ERA = u'9999.99'
            IHelper = float(IP.split('.')[0])
            IHelper += float(IP.split('.')[1])/3
            if IHelper == 0.0:
                GameERA = u'9999.99'
            else:
                GameERA = (int(ER)*9)/IHelper
                GameERA = unicode(round(GameERA,2))
            pitcherstats = (mlbplayerid,playername,playersurname,wlsh,IP,H,R,ER,BB,SO,HR,ERA,GameERA)
            homepitcherstats.append(pitcherstats)
        print awaypitcherstats
        print homepitcherstats
        return awaypitcherstats,homepitcherstats
            
            
            
            
        
        


if __name__ == u'__main__':
    a = MLBImportBoxscore0509(workingdir)